package com.weactivity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weactivity.dto.CommentDTO;
import com.weactivity.dto.SendCommentDTO;
import com.weactivity.dto.UserDTO;
import com.weactivity.entity.Comment;
import com.weactivity.mapper.CommentMapper;
import com.weactivity.mapper.UserMapper;
import com.weactivity.service.CommentService;
import com.weactivity.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<CommentDTO> findAllById(int aid) {

        List<CommentDTO> commentDTOList =  commentMapper.findAllById(aid);
        //获取用户在每个活动里面的身份
        for (CommentDTO i: commentDTOList) {
            int userId = i.getUserId();
            int role = userMapper.findRoleByAidAndUid(aid, userId);
            i.setRole(role);
        }


        return commentDTOList;
    }

    @Override
    public int sendComment(SendCommentDTO sendCommentDTO) {
        UserDTO user = UserHolder.getUser();

        int count = commentMapper.addComment(user.getId(), sendCommentDTO);


        return count;
    }
}
